<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>回调页面</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <meta name="format-detection" content="telephone=no">
    <meta name="renderer" content="webkit">
    <meta http-equiv="Cache-Control" content="no-siteapp"/>

    <link rel="stylesheet" href="../AmazeUI-2.4.2/assets/css/amazeui.css"/>
    <link href="../css/dlstyle.css" rel="stylesheet" type="text/css">
	<script src="./js/vue.js"></script>
	<script src="./js/axios.js"></script>
	<script src="./js/common.js"></script>
</head>
<body>
<div id="app">
</div>
</body>
<script type="text/javascript">
	var app =  new Vue({
		el:"#app",
		data:{
		},
		methods:{
			//封装公用方法 - 将字符串的参数解析为json对象
		    parseUrlParams2Obj(url) {//xxx?code=xxx&state=1
				let paramStr = url.substring(url.indexOf("?")+1);//code=xxx&state=1
				let paramArr = paramStr.split("&");//[code=xxx,state=1]
				let paramObj = {};
				for(let i = 0;i<paramArr.length;i++){
					let paramTemp = paramArr[i];//code=xxx
					let paramName = paramTemp.split("=")[0];//code
					let paramValue= paramTemp.split("=")[1];//xxx
					paramObj[paramName] = paramValue;//{code:xxx,state:1}
				}
			return paramObj;
			},
			getAccessToken(code){
				this.$http.get("/login/wechat/"+code).then(res =>{
                    if(!res.data.success){
                        //去绑定
                        var openId = res.data.resultObj
                        location.href = "bind.html?openId="+openId
                    }else {
                        let obj =  res.data.resultObj
                        sessionStorage.setItem('user', JSON.stringify(obj.user));
                        sessionStorage.setItem('token', obj.token);
                        //没有问题呢？
                        location.href = "index.html"
                    }



				})
			}
		},
		mounted(){
			//获取当前网页的地址
			let url = location.href
			let map = this.parseUrlParams2Obj(url)
			console.log(map.code)
			this.getAccessToken(map.code);
		}
	})
</script>
</html>